package me.sauce.folkpocket48.utils


import android.support.annotation.IdRes
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction


import android.support.v7.app.ActionBar
import android.support.v7.app.AppCompatActivity

/**
 * @author sauceWu
 * @since 2018/1/2 18:43
 */


fun AppCompatActivity.replaceFragmentInActivity(fragment: Fragment, frameId: Int) {
supportFragmentManager.beginTransaction().replace(frameId,fragment).commit()
}

/**
 * The `fragment` is added to the container view with tag. The operation is
 * performed by the `fragmentManager`.
 */
fun AppCompatActivity.addFragmentToActivity(fragment: Fragment, tag: String) {
    supportFragmentManager.beginTransaction().add(fragment,tag).commit()
}

fun AppCompatActivity.setupActionBar(@IdRes toolbarId: Int, action: ActionBar.() -> Unit) {
    setSupportActionBar(findViewById(toolbarId))
    supportActionBar?.run {
        action()
    }
}

//fun <T : ViewModel> AppCompatActivity.obtainViewModel(viewModelClass: Class<T>) =
//        ViewModelProviders.of(this, ViewModelFactory.getInstance(application)).get(viewModelClass)

/**
 * Runs a FragmentTransaction, then calls commit().
 */
private inline fun FragmentManager.transact(action: FragmentTransaction.() -> Unit) {
    beginTransaction().apply { action() }.commit()
}